From 0755491b777bd9119efd90149f83847158647f2e Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 7 Jun 2019 01:25:59 +0000 Subject: [PATCH] widget factory: Add a context menu example This is also taken from https://gitlab.gnome.org/Teams/Design/os-mockups/blob/master/menus/menu-design-patterns.png --- demos/widget-factory/widget-factory.c | 49 ++++++++++++++++++++- demos/widget-factory/widget-factory.ui | 59 +++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c index 2ccc659c1b..cd7e7cc20b 100644 --- a/demos/widget-factory/widget-factory.c +++ b/demos/widget-factory/widget-factory.c @@ -1606,6 +1606,37 @@ adjustment3_value_changed (GtkAdjustment *adj, GtkProgressBar *pbar) gtk_progress_bar_set_fraction (pbar, fraction); } +static void +clicked_cb (GtkGesture *gesture, + int n_press, + double x, + double y, + GtkPopover *popover) +{ + GdkRectangle rect; + + rect.x = x; + rect.y = y; + rect.width = 1; + rect.height = 1; + gtk_popover_set_pointing_to (popover, &rect); + gtk_popover_popup (popover); +} + +static void +set_up_context_popover (GtkWidget *widget, + GMenuModel *model) +{ + GtkWidget *popover = gtk_popover_new_from_model (widget, model); + GtkGesture *gesture; + + g_object_set (popover, "has-arrow", FALSE, NULL); + gesture = gtk_gesture_click_new (); + gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), GDK_BUTTON_SECONDARY); + g_signal_connect (gesture, "pressed", G_CALLBACK (clicked_cb), popover); + gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (gesture)); +} + static void activate (GApplication *app) { @@ -1619,6 +1650,7 @@ activate (GApplication *app) GtkWidget *dialog; GtkAdjustment *adj; GtkCssProvider *provider; + GMenuModel *model; static GActionEntry win_entries[] = { { "dark", NULL, NULL, "false", change_theme_state }, { "transition", NULL, NULL, "false", change_transition_state }, @@ -1636,7 +1668,11 @@ activate (GApplication *app) } accels[] = { { "app.about", { "F1", NULL } }, { "app.quit", { "q", NULL } }, + { "app.open", { "Return", NULL } }, { "app.open-in", { "n", NULL } }, + { "app.cut", { "x", NULL } }, + { "app.copy", { "c", NULL } }, + { "app.paste", { "v", NULL } }, { "win.dark", { "d", NULL } }, { "win.search", { "s", NULL } }, { "win.delete", { "Delete", NULL } }, @@ -1892,6 +1928,10 @@ activate (GApplication *app) widget = (GtkWidget *)gtk_builder_get_object (builder, "extra_info_entry"); g_timeout_add (100, (GSourceFunc)pulse_it, widget); + widget = (GtkWidget *)gtk_builder_get_object (builder, "box_for_context"); + model = (GMenuModel *)gtk_builder_get_object (builder, "new_style_context_menu_model"); + set_up_context_popover (widget, model); + gtk_widget_show (GTK_WIDGET (window)); g_object_unref (builder); @@ -1937,7 +1977,7 @@ select_action (GSimpleAction *action, GVariant *parameter, gpointer user_data) { - g_print ("Set action %s to %s\n", + g_print ("Select action %s value %s\n", g_action_get_name (G_ACTION (action)), g_variant_get_string (parameter, NULL)); @@ -1977,7 +2017,10 @@ main (int argc, char *argv[]) { "print", activate_action, NULL, NULL, NULL }, { "share", activate_action, NULL, NULL, NULL }, { "labels", activate_action, NULL, NULL, NULL }, + { "open", activate_action, NULL, NULL, NULL }, { "open-in", activate_action, NULL, NULL, NULL }, + { "open-tab", activate_action, NULL, NULL, NULL }, + { "open-window", activate_action, NULL, NULL, NULL }, { "cut", activate_action, NULL, NULL, NULL }, { "copy", activate_action, NULL, NULL, NULL }, { "paste", activate_action, NULL, NULL, NULL }, @@ -1987,6 +2030,10 @@ main (int argc, char *argv[]) { "broni", toggle_action, NULL, "true", NULL }, { "drutt", toggle_action, NULL, "true", NULL }, { "upstairs", toggle_action, NULL, "true", NULL }, + { "option-a", activate_action, NULL, NULL, NULL }, + { "option-b", activate_action, NULL, NULL, NULL }, + { "option-c", activate_action, NULL, NULL, NULL }, + { "option-d", activate_action, NULL, NULL, NULL }, }; gint status; diff --git a/demos/widget-factory/widget-factory.ui b/demos/widget-factory/widget-factory.ui index 0b6c2db8f1..93ba817f3f 100644 --- a/demos/widget-factory/widget-factory.ui +++ b/demos/widget-factory/widget-factory.ui @@ -3033,7 +3033,7 @@ microphone-sensitivity-medium-symbolic 1 - + 120 @@ -3830,4 +3830,61 @@ bad things might happen. + +
+ + Open + app.open + +
+
+ + Open in New Tab + app.open-tab + + + Open in New Window + app.open-window + +
+
+ + Cut + app.cut + + + Copy + app.copy + +
+
+ + Options +
+ + Option A + app.option-a + + + Option B + app.option-b + + + Option C + app.option-c + + + Option D + app.option-d + +
+
+
+
+ + Move to Trash + win.delete + +
+
-- 2.30.2